package com.xsh.gls.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xsh.gls.pojo.Terms;
import com.xsh.gls.pojo.Transfer;
import com.xsh.gls.pojo.User;
import com.xsh.gls.service.OrderService;
import com.xsh.gls.service.QuoteService;
import com.xsh.gls.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author shkstart
 * @date 2019/1/22 - 9:51
 */
@RestController
@RequestMapping("/money")
public class MoneyController {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private QuoteService quoteService;
    @Autowired
    private OrderService orderService;



    /*查询价格条款,将其分页显示*/
    @RequestMapping("/selectquote")
    public Object select(@RequestParam(required = false)String Destination, @RequestParam(required = false)Integer tranNo, @RequestParam(required = false)String pageNum,
                         HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        System.out.println(Destination+"\t"+tranNo);
        List<Terms> termsList=null;
        Integer _pageNum=1;
        if(pageNum!=null&&!pageNum.equals("")){
            _pageNum=Integer.parseInt(pageNum);
        }
        PageHelper.startPage(_pageNum,5, "MODIFYDATE desc");
        termsList = quoteService.selectAll(Destination,tranNo);
        System.out.println(termsList.size());
        PageInfo<Terms> page = new PageInfo<Terms>(termsList);



        return page;

    }



//    @RequestMapping("/myOrder")
//    public Object select(@RequestParam(required = false)String orderNo, @RequestParam(required = false)String pageNum,
//                         HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
//
//        response.setHeader("Access-Control-Allow-Origin",
//                request.getHeader("Origin"));
//        response.setHeader("Access-Control-Allow-Methods",
//                "POST, GET, OPTIONS, DELETE,PUT,PATCH");
//        response.setHeader("Access-Control-Max-Age", "3600");
//        response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
//        response.setHeader("Access-Control-Allow-Credentials", "true");
//        List<Order> orderList=null;
//        Integer _pageNum=1;
//        if(pageNum!=null&&!pageNum.equals("")){
//            _pageNum=Integer.parseInt(pageNum);
//        }
//        PageHelper.startPage(_pageNum,2, "CREATETIME desc");
//        orderList = orderService.selectByOrderNo(orderNo);
//        PageInfo<Order> page=new PageInfo<Order>(orderList);
//        /* List<Depot> list=page.getList();*/
//        return page;
//    }



    /*添加价格条款*/
    @RequestMapping("/addMoney")
    public Transfer select(@RequestParam(required = false)String Destination, @RequestParam(required = false)Integer tranNo, @RequestParam(required = false)BigDecimal unitPrice,
                           @RequestParam(required = false)BigDecimal fuel,
                           HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        Terms terms= new Terms();
        Transfer transfer = new Transfer();
        Terms terms2= quoteService.getByDTran(Destination,tranNo);
        if (terms2 != null){
            transfer.setMessage("no");
        }else {
            terms.setModifydate(new Date());
            terms.setFuel(fuel);
            terms.setTranno(tranNo);
            terms.setUnitprice(unitPrice);
            terms.setModifyno(((User)request.getAttribute("user")).getId());
            terms.setDestination(Destination);
            if (quoteService.insert(terms)){
                transfer.setMessage("yes");
            }else {
                transfer.setMessage("no");
            }
        }
        return transfer;
    }


    /*进行估价*/
    @RequestMapping("/gujia")
    public Transfer select(@RequestParam(required = false)String Destination, @RequestParam(required = false)Integer tranNo, @RequestParam(required = false) BigDecimal height,
                           HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        List<Terms> termsList=null;
        Transfer transfer = new Transfer();

        termsList = quoteService.selectAll(Destination,tranNo);
        BigDecimal money = null;
        if (termsList == null || termsList.size()==0){
            transfer.setMessage("no");
        }else {
            BigDecimal price =termsList.get(0).getUnitprice();
            BigDecimal fuel = termsList.get(0).getFuel();
            System.out.println(price+"\t"+fuel);
            money = (price.multiply(height)).divide(fuel,2,BigDecimal.ROUND_HALF_UP);
            transfer.setMessage(money.toString());
        }
        return transfer;
    }

    /*跳转到修改价格页面*/
    @RequestMapping("/showUpdateMoney")
    public Object select(@RequestParam(required = false)Integer id) throws UnsupportedEncodingException {
        System.out.println(id);
        Terms terms = quoteService.selectByPrimaryKey(id);
        System.out.println(terms.getDestination()+"\t"+terms.getFuel()+"\t"+terms.getUnitprice());
        return terms;
    }


    /*修改价格条款*/
    @RequestMapping("/updateMoney")
    public Transfer select(@RequestParam(required = false)Integer id,@RequestParam(required = false)String Destination, @RequestParam(required = false)Integer tranNo, @RequestParam(required = false)BigDecimal unitPrice,
                           @RequestParam(required = false)BigDecimal fuel, HttpServletRequest request) throws UnsupportedEncodingException {
        System.out.println(Destination+"\t"+tranNo+"\t"+unitPrice+"\t"+fuel);
        Transfer transfer = new Transfer();
        Terms terms = quoteService.selectByPrimaryKey(id);
        if (terms == null){
            transfer.setMessage("no");
        }else {
            terms.setUnitprice(unitPrice);
            terms.setTranno(tranNo);
            terms.setModifydate(new Date());
            terms.setFuel(fuel);
            terms.setDestination(Destination);
            terms.setModifyno(((User)request.getAttribute("user")).getId());
            if (quoteService.updateByPrimaryKey(terms)){
                transfer.setMessage("yes");
            }else {
                transfer.setMessage("no");
            }
        }

        return transfer;
    }

    /*删除价格条款*/
    @RequestMapping("/deleteMoney")
    public Transfer select(@RequestParam(required = false)Integer id, HttpServletRequest request) throws UnsupportedEncodingException {
        Transfer transfer = new Transfer();
        Terms terms = quoteService.selectByPrimaryKey(id);
        if (terms == null){
            transfer.setMessage("no");
        }else {
            if (quoteService.deleteByPrimaryKey(terms.getId())){
                transfer.setMessage("yes");
            }else {
                transfer.setMessage("no");
            }
        }
        return transfer;
    }
}
